//
//  LHSearchViewController.m
//  Knight_Tour
//
//  Created by Bruce_Lin on 15/5/18.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHSearchViewController.h"
#import "LHSearchMapView.h"
#import "LHMapViewController.h"
#import <BaiduMapAPI/BMapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MBProgressHUD.h"
#import "UIScrollView+MJRefresh.h"

@interface LHSearchViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate, BMKGeoCodeSearchDelegate>
@property (nonatomic, retain)LHSearchMapView *searchView;
@property (nonatomic, retain)BMKLocationService* locService;
@property (nonatomic, retain)BMKGeoCodeSearch *geoCode;
@property (nonatomic ,retain)BMKUserLocation *locations;
@property (nonatomic ,retain)NSString *address;
@end

@implementation LHSearchViewController

- (void)dealloc
{
    [_searchView release];
    [_geoCode release];
    [_locations release];
    [_locService release];
    [_address release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self creatSearchView];
    [self startLocation];
}


- (void)startLocation
{
    //设置定位精确度，默认：kCLLocationAccuracyBest
    [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    //指定最小距离更新(米)，默认：kCLDistanceFilterNone
    [BMKLocationService setLocationDistanceFilter:0.1f];
    
    //初始化BMKLocationService
    self.locService = [[[BMKLocationService alloc]init]autorelease];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
    
}

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    
    BMKReverseGeoCodeOption *option = [[[BMKReverseGeoCodeOption alloc] init]autorelease];
    // 将TextField中的数据传到反地址编码模型
    option.reverseGeoPoint = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    NSLog(@"%f - %f", option.reverseGeoPoint.latitude, option.reverseGeoPoint.longitude);
    // 调用反地址编码方法，让其在代理方法中输出
    NSLog(@"%d",[self.geoCode reverseGeoCode:option]);
}

#pragma mark 代理方法返回反地理编码结果
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (result) {
        NSLog(@"%@", result.address);
        
        self.address = result.address;
        self.searchView.corrcetAddress.text = result.address;
        
            }else {

                self.searchView.corrcetAddress.text = @"没找到你在哪...";
            }
}


- (void)creatSearchView
{
    self.searchView = [[LHSearchMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [_searchView setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
    _searchView.corrcetAddress.text = self.address;
    [self.view addSubview:_searchView];
    [_searchView release];
    self.searchView.corrcetAddress.text = @"正在定位....";
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [btn setFrame:CGRectMake(LHWidth - 120 , LHheight / 3 + 10, 120, 50)];//
    
    [btn.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:22]];
    [btn setTitle:@"Let's Go" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}

- (void)click
{
    LHMapViewController *map = [[LHMapViewController alloc]init];
    map.userLocations = self.locations;
    map.CityNameStr = self.searchView.cityNameTextField.text;
    map.keyWordName = self.searchView.KeyWordTextField.text;
    map.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:map animated:YES];
    [map release];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 此处记得不用的时候需要置nil，否则影响内存的释放
    _locService.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 不用时，置nil
    _locService.delegate = nil;
}

- (BMKGeoCodeSearch *)geoCode
{
    if (!_geoCode) {
        _geoCode = [[BMKGeoCodeSearch alloc] init];
        _geoCode.delegate = self;
    }
    return _geoCode;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
